package com.choudou5.solr.api;

import com.choudou5.solr.bean.common.DataTableBean;
import com.choudou5.solr.bean.common.PageResult;
import com.choudou5.solr.bean.common.SolrQueryParam;
import com.choudou5.solr.core.util.SolrHelper;
import com.choudou5.solr.framework.util.UserUtil;
import com.choudou5.solr.service.DicExtWordsService;
import com.choudou5.solr.service.SearchHiService;
import com.choudou5.solr.service.ServiceHelper;
import com.choudou5.solr.util.AssertUtil;
import com.choudou5.solr.util.SolrJUtil;
import org.apache.solr.core.SolrCore;

import java.util.List;

/**
 * Desc：搜索接口
 * Author：choudou5
 * Date：2018/7/17 0017
 */
public class SearchApi {

    private final SearchHiService searchHiService = ServiceHelper.singleton(SearchHiService.class);
    private final DicExtWordsService dicExtWordsService = ServiceHelper.singleton(DicExtWordsService.class);

    private SearchApi() {
    }
    private static class SingletonHolder{
        private static SearchApi instance = new SearchApi();

    }

    /**
     * 获取单例
     * @return
     */
    public static SearchApi getInstance() {
        return SingletonHolder.instance;
    }

    public PageResult searchPage(SolrQueryParam queryParam){
        SolrCore solrCore = SolrHelper.getCore(queryParam.getModule());
        AssertUtil.isNotNull(solrCore, "solr系统未初始化 " + queryParam.getModule());
        PageResult pageResult = SolrJUtil.findDocPage(queryParam, solrCore);
        searchHiService.saveAsync(queryParam, UserUtil.getUserIdStr(), pageResult);
        //异步分析
        dicExtWordsService.textDefAnalysis(queryParam.getKeyword());
        return pageResult;
    }

    public DataTableBean search(SolrQueryParam queryParam){
        PageResult pageResult = searchPage(queryParam);
        DataTableBean tableBean = new DataTableBean(pageResult.getPageNo());
        tableBean.setData(pageResult.getResult());
        tableBean.setRecordsFiltered(pageResult.getTotalCount());
        tableBean.setRecordsTotal(pageResult.getTotalCount());
        return tableBean;
    }

    public Long count(SolrQueryParam queryParam){
        SolrCore solrCore = SolrHelper.getCore(queryParam.getModule());
        AssertUtil.isNotNull(solrCore, "solr系统未初始化 " + queryParam.getModule());
        return SolrJUtil.findTotal(queryParam, solrCore);
    }

    public List<String> getFacetDataList(String field, SolrQueryParam queryParam){
        SolrCore solrCore = SolrHelper.getCore(queryParam.getModule());
        AssertUtil.isNotNull(solrCore, "solr系统未初始化 " + queryParam.getModule());
        return SolrJUtil.getFacetDataList(field, queryParam, solrCore);
    }

    public List<String> getFacetDateList(String field, SolrQueryParam queryParam, String dateFormat){
        SolrCore solrCore = SolrHelper.getCore(queryParam.getModule());
        AssertUtil.isNotNull(solrCore, "solr系统未初始化 " + queryParam.getModule());
        return SolrJUtil.getFacetDateList(field, queryParam, solrCore, dateFormat);
    }


    public List<String> getFacetDataValueKeyList(String field, SolrQueryParam queryParam){
        SolrCore solrCore = SolrHelper.getCore(queryParam.getModule());
        AssertUtil.isNotNull(solrCore, "solr系统未初始化 " + queryParam.getModule());
        return SolrJUtil.getFacetDataList(field, queryParam, solrCore, true);
    }


    /**
     * 优化索引
     * @param solrModule
     */
    public void optimize(String solrModule){


//        SolrCore solrCore = SolrHelper.getCore(query.getModule());
//        AssertUtil.isNotNull(solrCore, "solr系统未初始化 "+query.getModule());
//        PageResult pageResult = SolrJUtil.findPage(query, solrCore, classz);
    }


}
